<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the functions file
    require_once('functions.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
    include_once('classes/geshi/geshi.php');  //Geshi file
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['displayfile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Viewing file'));
    } else {
        //Connect to to the database using values provided by db.php
        $db = new my_db;
        $id = strip_tags(mysql_escape_string($_GET['id']));
         
        $db->query("SELECT * FROM trackedfiles WHERE id='$id'");
        $db->next_record();
        if ($db->nf() > 0) {
            $latestrev = strip_tags($db->f('latestrev'));
            $filename = strip_tags($db->f('filename'));
            $filepath = strip_tags($db->f('filepath'));
            $filetype = strip_tags($db->f('filetype'));
            $latestrev = mysql_escape_string($db->f('latestrev'));
            $lastrevdate = strip_tags($db->f('lastrevdate'));
            $lastuser = strip_tags($db->f('lastuser'));
             
            $mime = explode("/", $filetype);
            $filearray = explode('.', $filename);
            if (count($filearray) > 1) {
                $ep = count($filearray) -1;
                $extension = $filearray[$ep];
            }
            template_headtag("$filename");
            template_header();
            template_left();
            echo "<div class='pageheadertext'>$filename in</div>";
            echo "<div class='subheadertext'>$filepath</div><br/>";
                        
            $time = date("Y-m-j g:i");            echo "<div class='servertime'>Server time : $time";            echo "</div>";
            echo "<p>$filename was last edited on <b>$lastrevdate</b> by <b>$lastuser</b> </p>";             
            if ($_SESSION['userlevel'] >= $userlevel['history']) {
                    echo "<br /><a href='history.php?id=$id' title='History'><img src='images/tables/history.png' width='16' height='16' border='0' alt='History' /> Compare Versions in History</a>\n";
                }
            echo "<br /><br />";
            
            echo "<table class='table'>\n";
            $db->query("SELECT * FROM history WHERE filename='$filename' AND filepath='$filepath' ORDER BY revdate DESC");
            // keep getting the next row until there are no more to get
            while ($db->next_record()) {
                // Print out the contents of each row into a table
                $historyid = strip_tags($db->f('id'));
                $user = strip_tags($db->f('user'));
                $revdate = strip_tags($db->f('revdate'));
                $revhash = strip_tags($db->f('revhash'));
                $revsummary = strip_tags($db->f('revsummary'));
                $filetype = strip_tags($db->f('filetype'));
                if ($rowstyle == 'evenrow') {
                    $rowstyle = 'oddrow';
                } else {
                    $rowstyle = 'evenrow';
                }
                echo "\n<tr class='$rowstyle'>";
                echo "<td>$revdate</td>";
                echo "<td>";
                if ($user == "Unknown") {
                    echo "<A href='setuser.php?id=$historyid'>$user</a>";
                } else {
                    echo "$user";
                }
                echo "</td>
                    <td>$revsummary</td><td><a href='editsummary.php?id=$historyid'><span class='tinytext'><img src='images/tables/editsummary.png' width='16' height='16' border='0' alt='Edit Summary' title='Edit Summary' /></span></a>";
                echo "</td><td>";
                if ($row[revhash] !== '00000000000000000000000000000000') {
                    echo "<a href='view.php?id=$id&amp;rev=$revhash' title='View file'><img src='images/tables/view.png' width='16' height='16' border='0' alt='View' /></a>";
                }
                
                echo "</td></tr>\n";
                 
            }
            echo "</table><br />";
            
            echo "<div class='subheadertext2'>Current File Content : </div>";

             
             
             
            if ($mime[0] == "text") {
                $db->query("SELECT * FROM content WHERE dataid = '$latestrev'");
                if ($db->nf() > 0) {
                    $db->next_record();
                    $content = $db->f('content');
                    echo "<div class='basicbox'>\n";
                    
                $geshi = new GeSHi($content, $extension);
                
                $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS,5);
                $geshi->set_line_style('background: #fff;', 'background: #efefef;', true);
                echo $geshi->parse_code();

                    echo "</div><br />";
                     
                } else {
                    errorbox('Content not found!');
                }
            } else {
                 
                if ($mime[0] == "image") {
                    echo "<div class='imagebox'>";
                    echo "<img src='displaybin.php?id=$latestrev' border='0' /></div>";
                    echo "</div>";
                } else {
                    echo "<div class='downloadbox'><a href='displaybin.php?id=$latestrev'><img src='images/content/download.png' border='0' width='16' height='16' />&nbsp;Download File</a><br /></div>";
                }
            }
            echo "<br />";
             
            echo "<a href='javascript: history.go(-1)'>Back</a>";
            echo "<br /><br />";
        } else {
            errorpage('File not found. Invaild ID!', 'Viewing file');
        }
        template_footer();
         
         
    }
?>
